package io.renren.modules.tb.service.impl;

import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.tb.dao.HouseDao;
import io.renren.modules.tb.entity.HouseEntity;
import io.renren.modules.tb.service.HouseService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("houseService")
public class HouseServiceImpl extends ServiceImpl<HouseDao, HouseEntity> implements HouseService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        //获取登陆用户
        SysUserEntity userEntity = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal());

        LambdaQueryWrapper<HouseEntity> lambda = new LambdaQueryWrapper<>();
        if (!userEntity.isAdmin()) {
            //非超级管理员只能查询自己的房源
            lambda.eq(HouseEntity::getOwnerId, userEntity.getUserId());
        }

        IPage<HouseEntity> page = this.page(
                new Query<HouseEntity>().getPage(params),
                lambda
        );

        return new PageUtils(page);
    }

}
